<?php 
	include ("../../config/koneksi.php");
	$provinsi = "$_POST[provinsi]";
	$kabupaten = "$_POST[kabupaten]";
	$kecamatan = "$_POST[kecamatan]";
	
	if($provinsi == 'all' && $kabupaten == 'all' && $kecamatan == 'all'){
		$q = "select * from desa d inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten k on kc.id_kabupaten = k.id_kabupaten inner join propinsi p on k.id_propinsi = p.id_propinsi where k.aktif = 'Y' and p.aktif = 'Y' order by d.id_desa";
		$r = mysql_query($q);
		$content = "DAFTAR DESA\n\nNO;KODE;DESA;KECAMATAN;KABUPATEN;PROVINSI\n";
		while ($data = mysql_fetch_array($r)) {
			$no++;
			$content .= "$no;$data[id_desa];$data[desa];$data[kecamatan];$data[kabupaten];$data[propinsi]\n";
		};
	}else if($provinsi != 'all' && $kabupaten == 'all' && $kecamatan == 'all'){
		$q = "select * from desa d inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten k on kc.id_kabupaten = k.id_kabupaten inner join propinsi p on k.id_propinsi = p.id_propinsi where k.id_propinsi = '$provinsi' and k.aktif = 'Y' and p.aktif = 'Y' order by d.id_desa";
		$prov = mysql_fetch_array(mysql_query("select propinsi from propinsi where id_propinsi = '$provinsi'"));
		$r = mysql_query($q);
		$content = "DAFTAR DESA DI PROVINSI ".strtoupper($prov['propinsi'])."\n\nNO;KODE;DESA;KECAMATAN;KABUPATEN\n";
		while ($data = mysql_fetch_array($r)) {
			$no++;
			$content .= "$no;$data[id_desa];$data[desa];$data[kecamatan];$data[kabupaten]\n";
		};
	}else if($provinsi != 'all' && $kabupaten != 'all' && $kecamatan == 'all'){
		$q = "select * from desa d inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten k on kc.id_kabupaten = k.id_kabupaten inner join propinsi p on k.id_propinsi = p.id_propinsi on k.id_propinsi = p.id_propinsi where k.id_kabupaten = '$kabupaten' and k.aktif = 'Y' and p.aktif = 'Y' order by d.id_desa";
		$prov = mysql_fetch_array(mysql_query("select propinsi from propinsi where id_propinsi = '$provinsi'"));
		$kab = mysql_fetch_array(mysql_query("select kabupaten from kabupaten where id_kabupaten = '$kabupaten'"));
		$r = mysql_query($q);
		$content = "DAFTAR DESA DI PROVINSI ".strtoupper($prov['propinsi'])."\nKABUPATEN ".strtoupper($kab['kabupaten'])."\n\nNO;KODE;DESA;KECAMATAN\n";
		while ($data = mysql_fetch_array($r)) {
			$no++;
			$content .= "$no;$data[id_desa];$data[desa];$data[kecamatan]\n";
		};
	}else{
		$q = "select * from desa where id_kecamatan = '$kecamatan' order by id_desa";
		$prov = mysql_fetch_array(mysql_query("select propinsi from propinsi where id_propinsi = '$provinsi'"));
		$kab = mysql_fetch_array(mysql_query("select kabupaten from kabupaten where id_kabupaten = '$kabupaten'"));
		$kec = mysql_fetch_array(mysql_query("select kecamatan from kecamatan where id_kecamatan = '$kecamatan'"));
		$r = mysql_query($q);
		$content = "DAFTAR DESA DI PROVINSI ".strtoupper($prov['propinsi'])."\nKABUPATEN ".strtoupper($kab['kabupaten'])."\nKECAMATAN ".strtoupper($kec['kecamatan'])."\n\nNO;KODE;DESA\n";
		while ($data = mysql_fetch_array($r)) {
			$no++;
			$content .= "$no;$data[id_desa];$data[desa]\n";
		};
	}
	//echo $q;
	header("Content-type: text/csv");
	header("Content-Disposition: attachment; filename=desa.csv");
	header("Pragma: no-cache");
	header("Expires: 0");
	echo $content;

?>